package queue_stack.leetcode.easy;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

/**
 * @author bruin_du
 * @description 包含min函数的栈
 * @date 2022/5/16 16:53
 **/
public class Offer30_MinStack {
    private Deque<Integer> stack;
    private Deque<Integer> minStack;

    public Offer30_MinStack() {
        stack = new ArrayDeque<>();
        minStack = new ArrayDeque<>();
    }

    public void push(int x) {
        if (minStack.isEmpty() || minStack.peekLast() >= x)
            minStack.offerLast(x);
        stack.offerLast(x);
    }

    public void pop() {
        if (stack.isEmpty())
            return;
        if (stack.pollLast().equals(minStack.peekLast()))
            minStack.pollLast();
    }

    public int top() {
        if (stack.isEmpty())
            return -1;
        return stack.peekLast();
    }

    public int min() {
        if (stack.isEmpty())
            return -1;
        return minStack.peekLast();
    }
}
